#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import asyncore
import logging
import time

from net.netcli import EventType
from net.netcli import MsgCtx
from net.netcli import NetAdapter
from net.netcli import NetCli

logging.getLogger().setLevel(logging.INFO)


class TestAdapter(NetAdapter):

    def handle_data(self, handler, data):
        logging.debug(data)
        ctx = MsgCtx.ctx_from_json(data)
        if ctx.cmd == EventType.INIT:
            self.do_init(handler=handler, data=ctx.data)
        if ctx.cmd == EventType.DATA:
            self.do_print(handler=handler, data=ctx.data)

    def do_init(self, handler, data):
        logging.info('session_id=%s', data)

    def do_print(self, handler, data):
        logging.info(data)


if __name__ == '__main__':
    cli = NetCli(host='localhost', port=10112, adapter=TestAdapter())
    cli.start()
    names = ['Cathy', 'Julie', 'Tenchael']
    for _name in names:
        cli.handler.send_cmd(cmd=EventType.DATA, data=_name)
    asyncore.loop()
    t = threading.Thread(target=worker)
    threads.append(t)
    t.start()
    # time.sleep(3)
    logging.info('end')
